<?php
require_once APPLICATION_PATH . '/controllers/LoginController.php';

/**
 * 报价单）控制器
 * @author Administrator
 *
 */
class QuoteController extends LoginController
{

    public function init()
    {
        parent::init();
    }

    /**
     * 列表
     * @see LoginController::indexAction()
     */
    public function indexAction()
    {    	
    	$quoteM = new Application_Model_DbTable_Quote();
    	
    	//删除临时付费项目
        $quoteM->deleteTempItem();
        
        //跟进数据
        $isfollowup = $this->getRequest()->getParam("isfollowup");
        
        if($this->getRequest()->isPost()){
        	//获取数据
	    	$portM  = new Application_Model_DbTable_Port();
	    	$custM  = new Application_Model_DbTable_Customer();    	
	    	$userM  = new Application_Model_DbTable_User();
	    	$currM  = new Application_Model_DbTable_Currency();
	    	
	    	$portKV = $portM->getKvIdNameEn();
	    	$custKV = $custM->getKvIdName();
	    	$userKV = $userM->getKVnameid();
	    	$currKV = $currM->getKvIdName();
	    	
	    	$where = "";
	    	if(!empty($isfollowup)){
	    		$nowDate = date("Y-m-d",time());
	    		$where = " `remind_alarm_time` = '{$nowDate}'";
	    	};
	    	
        	$quoteM = new Application_Model_DbTable_Quote();
        	$pageM = $this->_helper->Flexgp();
			$data = $quoteM->jqfetchAll($pageM,$where);
			$rdata= $data["data"];
			foreach ($rdata as &$d){
				$d["clerk"] 			= !empty($userKV[$d["clerk"]]) ? $userKV[$d["clerk"]] : "";
				$d["customer"] 			= !empty($custKV[$d["customer"]]) ? $custKV[$d["customer"]] : "";
				$d["currency"] 			= !empty($currKV[$d["currency"]]) ? $currKV[$d["currency"]] : "";
				$d["port_of_shipmont"] 	= !empty($portKV[$d["port_of_shipmont"]]) ? $portKV[$d["port_of_shipmont"]] : "";
				$d["destination"] 		= !empty($portKV[$d["destination"]]) ? $portKV[$d["destination"]] : "";
				$d["create_user"] 		= !empty($userKV[$d["create_user"]]) ? $userKV[$d["create_user"]] : "";
				$d["create_time"] 		= date("Y-m-d H:i:s",$d["create_time"]);
			}
			$data = $this->view->jqflexgrid($data,"id");
		    $this->_helper->json($data); 
        }else{
        	//获取页面
    		$this->view->title = "报价单列表";
        }
    }

	/** 
     * 跟进列表
     * @see LoginController::indexAction()
     */
    public function followupAction()
    {    	
    	$this->view->isfollowup = "index/isfollowup";
    	$this->view->title = "今日需要跟进";
    	$this->render("index");
    }

    
    
    /**
     * 添加
     */
    public function addAction()
    {
    	$formM = new Application_Form_Base("quote");
    	$quoteM = new Application_Model_DbTable_Quote();
    	$quoteM->setMultiOptions($formM);
    	$formM->setAction($this->view->pagePath . "/quote/edit");
    	
    	if($this->getRequest()->isPost()){
        	//提交数据
        	//json返回的数据
        	$reData = array(
        		"status" => 0,
        		"msg"	 => "失败。"
        	);
        	$data = $this->getRequest()->getParams();
        	if($formM->isValid($data)){
        		$quoteM = new Application_Model_DbTable_Quote();
        		$data  = $formM->getValues(); 
        		//$data["id"] = $data["userid"];unset($data["userid"]);
        		$data['create_user'] = $this->_port_id;	//创建人id
	        	$data['create_time'] = time(); 			//创建时间
	        	try {
		        	$result = $quoteM->insert($data);
		        	$reData["status"]=1;
		        	$reData["msg"]="成功！";
		        	Application_Service_Msg::write(
		        		"{$this->_user_name}添加报价单：{$data["name"]}",
		        		Application_Model_Msg::TYPE_USERDO_LOG
		        	);
	        	}catch (Exception $e){
        			$reData["form"] = $formM->render();
	        		$reData["msg"]="数据库错误！" . $e->getMessage();
	        	}
        	}else{
        		$reData["msg"]="表单填写有误！";
        		$reData["form"] = $formM->render();
        	}
        	$this->_helper->json($reData);
        }else{
        	//获取表单
        	$data = array();
        	$data['create_user'] = $this->_user_id;	//创建人id
        	$data['create_time'] = time(); 			//创建时间
        	$data['temp_state']  = 1; 				//临时
        	
        	$id = $quoteM->insert($data);
        	$idE = $formM->getElement("id");
        	$idE->setValue($id);
        	
        	Application_Service_Msg::write(
        		"{$this->_user_name} 准备新建报价单：{$id}",
        		Application_Model_Msg::TYPE_USERDO_LOG
        	);
        	
    		$this->view->form = $formM;
        }
        //$this->_helper->layout()->disableLayout();
        //$this->renderScript("zendform.phtml");
    }
    
    /**
     * 编辑
     * @see LoginController::indexAction()
     */
    public function editAction()
    {
    	$id = intval($this->getRequest()->getParam("id",0));
    	$formM = new Application_Form_Base("quote");
    	$formM->setAttrib("id", "quote_form");
    	$quoteM = new Application_Model_DbTable_Quote();
    	$quoteM->setMultiOptions($formM);
        $formM->setAction($this->view->pagePath . "/quote/edit");
    	
    	if($this->getRequest()->isPost()){
        	//提交数据
        	$reData = array(
        		"status" => 0,
        		"msg"	 => "失败。"
        	);
        	$data = $this->getRequest()->getParams();
        	if($formM->isValid($data)){
        		
        		$data  = $formM->getValues(); 
        		unset($data["userid"]);
        		$data["temp_state"] = 0;
	        	try {
		        	$result = $quoteM->update($data, " `id` = {$id} ");
		        	$reData["status"] = 1;
		        	$reData["approve_status"] = "0"; //变成未审核状态
		        	$reData["msg"]="成功！";
		        	Application_Service_Msg::write(
		        		"{$this->_user_name} 修改报价单：{$data["id"]}",
		        		Application_Model_Msg::TYPE_USERDO_LOG
		        	);
	        	}catch (Exception $e){
        			$reData["form"] = $formM->render();
	        		$reData["msg"]="数据库错误！" . $e->getMessage();
	        	}
        	}else{
        		$reData["msg"]="表单填写有误！";
        		$reData["form"] = $formM->render();
        	}
        	$this->_helper->json($reData);
        }else{
        	//获取表单
        	$data = $quoteM->fetchRow(" `id` = {$id} ");
        	if($data){
        		$formM->setDefaults($data->toArray());
        	}
    		$this->view->form = $formM;
        }
    }
    
	/**
	 * 删除
	 */
	public function delAction()
	{
		$reData = array(
			"status" => 0,
			"msg"    => "参数错误"
		);	    
    	$id = (array)$this->getRequest()->getParam('id');
    	if($id){
			$quoteM = new Application_Model_DbTable_Quote();
			$where = $quoteM->getAdapter()->quoteInto('`id` IN(?)', $id);
    		if($result = $result = $quoteM->delete($where)){
    			$reData = array(
					"status" => 1,
    		 		"msg"    => "成功删除{$result}条数据！"
    		 	);
    		 	$id = implode(",",$id);
    		 	Application_Service_Msg::write(
	        		"{$this->_user_name} 删除报价单：{$id}",
	        		Application_Model_Msg::TYPE_USERDO_LOG
	        	);
    		 }
	    }else{
	        	
	    }
	    $this->_helper->json($reData);
   }

	/**
	 * 打印和预览
	 */
	public function printAction(){
		try{
			$quoteid = intval($this->getRequest()->getParam("id",0));
			$quoteM  = new Application_Model_DbTable_Quote();
			$quotePM = new Application_Model_DbTable_Quoteproduct();
			$compyM  = new Application_Model_DbTable_Companyinfo();
			$custoM  = new Application_Model_DbTable_Customer();
			
			$rsQuote  = $quoteM->fetchRow(" `id` = {$quoteid} ");
			$reQuoteP = $quotePM->fetchAll(" `quote_id` = {$quoteid}  ");
			$reCompny = $compyM->fetchRow();
			$reCustom = $custoM->fetchRow(" `id`={$rsQuote->customer} ");
			
			$this->view->quote    = $rsQuote;
			$this->view->products = $reQuoteP->toArray();
			$this->view->cominfo  = $reCompny;
			$this->view->customer = $reCustom;
		}
		catch (Exception $e){
			throw $e;
//			die("404");
		}
		$this->_helper->layout()->disableLayout();
	}
   
	/**
	 * 某个报价单的产品列表
	 */
	public function listproductAction()
	{
		$quoteid = intval($this->getRequest()->getParam("quoteid",0));
		$quoteprdM = new Application_Model_DbTable_Quoteproduct();
		$pageM = $this->_helper->Flexgp();
		$data = $quoteprdM->jqfetchAll($pageM," `quote_id` = {$quoteid} ");
		$data = $this->view->jqflexgrid($data,"id");
	    $this->_helper->json($data); 
	}
   
	/**
	 * 添加产品
	 */
	public function addproductAction()
	{
		$quoteid = intval($this->getRequest()->getParam("quoteid",0));
		$formM = new Application_Form_Base("quote_product");
		$quoteM = new Application_Model_DbTable_Quoteproduct();
    	$quoteM->setMultiOptions($formM);
    	
		if($this->getRequest()->isPost())
		{
			//请求数据
			//json返回的数据
        	$reData = array(
        		"status" => 0,
        		"msg"	 => "失败。"
        	);
        	$data = $this->getRequest()->getParams();
        	if($formM->isValid($data)){
        		$data  = $formM->getValues();
	        	try {
		        	$result = $quoteM->insert($data);
		        	$reData["status"]=1;
		        	$reData["msg"]="成功！";
	        	}catch (Exception $e){
        			$reData["form"] = $formM->render();
	        		$reData["msg"]="数据库错误！" . $e->getMessage();
	        	}
        	}else{
        		$reData["msg"]="表单填写有误！";
        		$reData["form"] = $formM->render();
        	}
        	$this->_helper->json($reData);
		}else
		{
			//请求页面
			if(!$quoteid)die(404);
			$formM->setAction($this->view->pagePath . "/quote/addproduct");
			$quidE = $formM->getElement("quote_id");
			$quidE->setValue($quoteid);
			$this->view->form = $formM;
		}
		$this->_helper->layout()->disableLayout();
        $this->renderScript("zendform.phtml");
	}
	
	/**
	 * 编辑产品
	 */
	public function editproductAction()
	{
		$id = intval($this->getRequest()->getParam("id",0));
		$formM = new Application_Form_Base("quote_product");
		$quoteM = new Application_Model_DbTable_Quoteproduct();
    	$quoteM->setMultiOptions($formM);
		$formM->setAction($this->view->pagePath . "/quote/editproduct");
    	
		if($this->getRequest()->isPost())
		{
			//请求数据
			//json返回的数据
        	$reData = array(
        		"status" => 0,
        		"msg"	 => "失败。"
        	);
        	$data = $this->getRequest()->getParams();
        	if($formM->isValid($data)){
        	$data  = $formM->getValues(); 
        		unset($data["id"]);
	        	try {
		        	$result = $quoteM->update($data, " `id` = {$id} ");
		        	$reData["status"]=1;
		        	$reData["msg"]="成功！";
	        	}catch (Exception $e){
        			$reData["form"] = $formM->render();
	        		$reData["msg"]="数据库错误！" . $e->getMessage();
	        	}
        	}else{
        		$reData["msg"]="表单填写有误！";
        		$reData["form"] = $formM->render();
        	}
        	$this->_helper->json($reData);
		}else
		{
			//请求页面
			if(!$id)die(404);
			$data = $quoteM->fetchRow(" `id` = {$id} ");
        	if($data){
        		$formM->setDefaults($data->toArray());
        	}
			$this->view->form = $formM;
		}
		$this->_helper->layout()->disableLayout();
        $this->renderScript("zendform.phtml");
	}
}

